//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.BlogAdmin
{
	/// <summary>
	///		Summary description for GroupListControl.
	/// </summary>
	public class GroupListControl : BaseBlogAdminGridControl
	{
	
		#region Child Controls

		protected CA.Grid Grid1;
		protected CheckBox chkConfirmInsert;
		protected CheckBox chkConfirmUpdate;
		protected CheckBox chkConfirmDelete;
		protected ResourceControl Resourcecontrol1;
		protected ModalLink NewGroup;

		#endregion

		private bool IsEdit = true;

		private const string EditGroupUrl = "GroupForm.aspx?GroupID={0}";


		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
			Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}
		
		
		private void Page_Load(object sender, EventArgs e)
		{

			NewGroup.Text = Components.ResourceManager.GetString("CP_BlogAdmin_GroupList_AddNew");
			NewGroup.Url = string.Format(EditGroupUrl,"-1");

			if(!Page.IsPostBack && !BlogAdminPage.IsCallBack)
			{

				buildGrid();
				Grid1.DataBind();
			}
		}


		private void UpdateDb(CA.GridItem item, string command)
		{

			//Check to see that the user can administer
			//Ideally we will add conditional formatting on the grid to hide these options too
			if(!CSContext.Current.User.IsGalleryAdministrator)
				throw new CSException( CSExceptionType.AdministrationAccessDenied );

			this.IsEdit = true;
			Group group;

			switch (command)
			{
			
				case "DELETE":
					
					group = WeblogGroups.GetWeblogGroup(int.Parse(item["GroupID"].ToString()), false, true, true);
					if(HasChildSections(group.GroupID))
						throw new ReadOnlyException(ResourceManager.GetString("CP_BlogAdmin_GroupList_GroupNotEmptyException"));

					Groups.DeleteGroup(group);

					//flush the cache to make sure we pick up any changes
					WeblogGroups.GetWeblogGroups(false,true,true); 

					break;

				default: 
					throw new NotImplementedException(string.Concat("The {0} command has not yet been implemented!" ,command)); 
			}
		}

		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			UpdateDb(e.Item, "DELETE"); 
		}

		protected override void buildGrid()
		{
			ArrayList groups = GetGroups();


			//The grid will only show checkboxes in edit mode if the data source is a DataTable
			Grid.DataSource = CADataConverter.ToDataTable(groups, typeof(Group));
			this.RecordCount = groups.Count;

			ApplyUserSettings();

		}

		protected override void ConfigureGrid()
		{
			AddGridPagerTemplate("Name");
			AddGridHrefTemplate(@"Blogs.aspx?GroupID=## DataItem.GetMember(""GroupID"").Text ##","Name");
		}

		protected ArrayList GetGroups()
		{
			return WeblogGroups.GetWeblogGroups(false, true, this.IsEdit);
		}

		/// <summary>
		/// Returns true if the Group has Sections
		/// </summary>
		protected bool HasChildSections(int groupID)
		{
			return Weblogs.GetWeblogsByGroupID(groupID,true,false,false).Count > 0;
		}

	}
}
